package com.pl.museumguide.activities;

import java.io.File;
import com.pl.museumguide.R;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class Wyswietl extends Activity {

	File[] files;
	Uri mp3Uri;
	MediaPlayer mp;
	String selection = "NULL";
	File mp3;
	String button = "NULL";
	String playing = "NULL";

	
	void play() {	//funkcja sprawdza czy wcisnelismy ten sam guzik i dokonuje odpowiednich opcji
			if(button.equals(playing)){
				if(mp.isPlaying()){
					mp.pause();
				}
				else{
					mp.start();
				}
			}
			else{
				mp.reset();
				runFile();
				}
				
		}
	
	void runFile() {	//funkcja ktora wczytuje plik i odpala audio
		boolean exist = new File(Environment.getExternalStorageDirectory()
				+ File.separator + "MuseumGuide" + File.separator + "Zabytki"
				+ File.separator + selection + File.separator + "audio", button + ".mp3").exists();
		if (exist) {
			mp3 = new File(Environment.getExternalStorageDirectory()
					+ File.separator + "MuseumGuide" + File.separator
					+ "Zabytki" + File.separator + selection + File.separator
					+ "audio", button + ".mp3");
			mp3Uri = Uri.fromFile(mp3);
			mp = MediaPlayer.create(Wyswietl.this, mp3Uri);
			mp.start();
			playing = button;
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wyswietl);
		TextView nazwa = (TextView) findViewById(R.id.g_head);
		Bundle extras = getIntent().getExtras();
		selection = extras.getString("nazwa");
		
		final File f = new File(Environment.getExternalStorageDirectory()
				+ File.separator + "MuseumGuide" + File.separator + "Zabytki"
				+ File.separator + selection + File.separator + "pics");
		nazwa.setText(selection);
		files = f.listFiles();
		
		ImageButton playpl = (ImageButton) findViewById(R.id.playPL);
		ImageButton playen = (ImageButton) findViewById(R.id.playEN);
		ImageButton playde = (ImageButton) findViewById(R.id.playDE);
		
		mp = MediaPlayer.create(Wyswietl.this,Uri.EMPTY);
		
		mp.setOnCompletionListener(new OnCompletionListener(){
			public void onCompletion(MediaPlayer mp) {
				mp.seekTo(0);
			}
		});
		
		playpl.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				button = "pl";
				play();
				
			}
		});

		playen.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				button = "en";
				play();
			}
		});

		playde.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				button = "de";
				play();
			}
		});

		Gallery gallery = (Gallery) findViewById(R.id.gallery);
		gallery.setAdapter(new ImageAdapter(this));
		gallery.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				// ---display the images selected---
				ImageView imageView = (ImageView) findViewById(R.id.image);
				File[] files = f.listFiles();
				Bitmap bitmap = BitmapFactory.decodeFile(files[position]
						.getPath());
				imageView.setImageBitmap(bitmap);

			}
		});
	}

	public class ImageAdapter extends BaseAdapter {
		private Context context;
		private int itemBackground;

		public ImageAdapter(Context c) {
			context = c;
			// ---setting the style---
			TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
			itemBackground = a.getResourceId(
					R.styleable.Gallery_android_galleryItemBackground, 0);
			a.recycle();
		}

		// ---returns the number of images---
		public int getCount() {
			//
			return files.length;
		}

		// ---returns the ID of an item---
		public Object getItem(int position) {
			return position;
		}

		// ---returns the ID of an item---
		public long getItemId(int position) {
			return position;
		}

		// ---returns an ImageView view---
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView = new ImageView(context);
			Bitmap bitmap = BitmapFactory.decodeFile(files[position].getPath());
			imageView.setImageBitmap(bitmap);
			imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
			imageView.setBackgroundResource(itemBackground);
			return imageView;
		}
	}

}